<?php 
class paging {
	var $rowperpage;
	var $pageperstage;
	var $mode;

	var $FirstPage;
	var $PreviousStage;
	var $Previous;
	var $Next;
	var $NextStage;
	var $LastPage;
	var $phpself;
	
	function setPhpself($filename){
		$this->phpself = $filename;
	}

	function paging ($rowperpage,$pageperstage,$mode) {
		$this->rowperpage = $rowperpage;
		$this->pageperstage = $pageperstage;
		$this->mode = $mode;
		$this->getMode();
	}
	function getMode(){
		switch($this->mode){
			case 'map':
				$this->FirstPage = "1";
				$this->PreviousStage = "&laquo;"; //<<
				$this->Previous = "&lsaquo;";//<
				$this->Next = "&rsaquo;";//>
				$this->NextStage = "&raquo;";//>>
				$this->LastPage = "Last Page";
				break;
			case 'txt':
				$this->FirstPage = "First Page";
				$this->PreviousStage = "Previous Stage";
				$this->Previous = "Previous";
				$this->Next = "Next";
				$this->NextStage = "Next Stage";
				$this->LastPage = "Last Page";
				break;
		}
	}
	function getPaging($rownum, $page="1", $stage="1") {
		$qs = "";
		$arr = explode("&",$_SERVER["QUERY_STRING"]);
		if (is_array($arr)) {
			for ($i=0;$i<count($arr);$i++) {
				if (!is_int(strpos($arr[$i],"page=")) && !is_int(strpos($arr[$i],"stage=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
			}
		}

		if ($this->rowperpage<$rownum) {
			$allpage = ceil($rownum/$this->rowperpage);
			$allstage  = ceil($allpage/$this->pageperstage);
			$minpage = (($stage-1)*$this->pageperstage)+1;
			$maxpage = $stage*$this->pageperstage;
			if ($maxpage>$allpage) $maxpage = $allpage;
			if ($allpage>1) {
				//$rtn = "<table width=100% align=center cellpadding=2 cellspacing=0><tr align=center valign=middle><td class=\"smallbody\">";
				
				$rtn = "<div class=\"pagination\"><ul>\n";
				$rtn .= "<li class=\"disablepage\">page </li>\n";

				if ($page>1 && $stage>1) $rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=1&stage=1\" title='Page 1'>".$this->FirstPage."</a></li>\n ";
				if ($stage>1) $rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($minpage-1)."&stage=".($stage-1)."\" title='Page ".($minpage-1)."'>".$this->PreviousStage."</a> </li>\n";
				if ($page>1) {
					if ($page==$minpage) {
						$rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($page-1)."&stage=".($stage-1)."\" title='Page ".($page-1)."'>".$this->Previous."</a> </li>\n";
					} else {
						$rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($page-1)."&stage=$stage\" title='Page ".($page-1)."'>".$this->Previous."</a> </li>\n";
					}
				}
				for ($i=$minpage;$i<=$maxpage;$i++) {

					if ($i==$page) {
						//$rtn .= "<b>$i</b> | ";
						$rtn .= "<li class=\"currentpage\">$i </li>\n";
						
					} else {
						$rtn .= "<li><a href=\"".$phpself."?".$qs."page=$i&stage=$stage\" title='Page $i'>$i</a> </li>\n";
					}

				}
				if ($page<=$maxpage) {
					if ($page==$maxpage && $stage<$allstage) {
						$rtn .= " <li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($page+1)."&stage=".($stage+1)."\" title='Page ".($page+1)."'>".$this->Next."</a> </li>\n";
					} elseif ($page<$maxpage) {
						$rtn .= " <li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($page+1)."&stage=$stage\" title='Page ".($page+1)."'>".$this->Next."</a> </li>\n";
					}
				}
				if ($stage<$allstage) $rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".($maxpage+1)."&stage=".($stage+1)."\" title='Page ".($maxpage+1)."'>".$this->NextStage."</a> </li>\n";

				if($this->mode=="map") $this->LastPage = $allpage;

				if ($page<$allpage) $rtn .= "<li class=\"nextpage\"><a href=\"".$phpself."?".$qs."page=".$allpage."&stage=".$allstage."\" title='Page ".($allpage)."'>".$this->LastPage."</a> </li> \n";

				$rtn .= "<li class=\"disablepage\">&bull; $rownum item </li>\n";

				$rtn = substr($rtn,0,strlen($rtn)-3);

				//$rtn .= "</td></tr></table>";
				$rtn .= "</ul></div>";
				return $rtn;
			}
		}
	}

};
//include_once("../lib/paging.class.php");
//$test = new paging(5,5,"map");
//empty($_GET['page'])?$page=1:$page = $_GET['page'];
//empty($_GET['stage'])?$stage = 1:$stage = $_GET['stage'];
//echo  $test->getPaging(200,$page,$stage);